import crypto from "crypto-js";
class AesUtil{
    public static getKeyByTime(time:string|number){//通过时间获得key
        if(!(typeof time=="string")) time=JSON.stringify(time);
        return crypto.MD5(time).toString().substring(0,16);
    }
    //加密为base64的字符串
    public static encrypt(data:string,key:string){
        if(data==""||data===null) return "";
        if(!(typeof data=="string")) data=JSON.stringify(data);
        let wordArray=crypto.enc.Utf8.parse(key);
        return crypto.AES.encrypt(data,wordArray,{
            mode: crypto.mode.ECB,
            padding: crypto.pad.Pkcs7
        }).toString();
    }
    //解密base64的字符串
    public static decrypt(data:string,key:string){
        if(data==""||data===null) return "";
        if(!(typeof data=="string")) data=JSON.stringify(data);
        let wordArray=crypto.enc.Utf8.parse(key);
        return crypto.AES.decrypt(data,wordArray,{
            mode: crypto.mode.ECB,
            padding: crypto.pad.Pkcs7
        }).toString(crypto.enc.Utf8);
    }
}
export default AesUtil;